package cn.fan.whale.utils;

import cn.fan.whale.common.GlobalVal;
import cn.hutool.core.codec.Base64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

public class ValidateCodeUtils {

    private static final String RANDOM_BASIC = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";//随机产生数字与字母组合的字符串
    private static final int WIDTH = 95;// 图片宽
    private static final int HEIGHT = 25;// 图片高
    private static final int LINE_SIZE = 40;// 干扰线数量
    private static final int CODE_LENGTH = 4;// 随机产生字符数量
    private static final Random RANDOM = new Random();

    /**
     * 获得字体
     */
    private static Font getFont() {
        return new Font("Fixedsys", Font.BOLD, 18);
    }

    /**
     * 获得颜色
     */
    private static Color getRandColor(int fc, int bc) {
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + RANDOM.nextInt(bc - fc - 16);
        int g = fc + RANDOM.nextInt(bc - fc - 14);
        int b = fc + RANDOM.nextInt(bc - fc - 18);
        return new Color(r, g, b);
    }

    /**
     * 生成随机图片
     */
    public static String getRandomCode(String key) throws IOException {

        // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);

        // 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
        Graphics g = image.getGraphics();
        g.fillRect(0, 0, WIDTH, HEIGHT);//图片大小
        g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//字体大小
        g.setColor(getRandColor(110, 133));//字体颜色

        // 绘制干扰线
        for (int i = 0; i <= LINE_SIZE; i++) {
            drawLine(g);
        }

        // 绘制随机字符
        String randomString = "";

        for (int i = 1; i <= CODE_LENGTH; i++) {
            randomString = drawString(g, randomString, i);
        }
        GlobalVal.VALIDATE_CODE_MAP.put(key, randomString);
        g.dispose();

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//io流
        ImageIO.write(image, "JPEG", byteArrayOutputStream);//写入流中
        byte[] bytes = byteArrayOutputStream.toByteArray();//转换成字节
        String validateCodeBase64 = Base64Encoder.encode(bytes);
        validateCodeBase64 = validateCodeBase64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        return "data:image/jpg;base64," + validateCodeBase64;

    }

    /**
     * 绘制字符串
     */
    private static String drawString(Graphics g, String randomString, int i) {
        g.setFont(getFont());
        g.setColor(new Color(RANDOM.nextInt(101), RANDOM.nextInt(111), RANDOM
                .nextInt(121)));
        String rand = String.valueOf(getRandomString(RANDOM.nextInt(RANDOM_BASIC
                .length())));
        randomString += rand;
        g.translate(RANDOM.nextInt(3), RANDOM.nextInt(3));
        g.drawString(rand, 13 * i, 16);
        return randomString;
    }

    /**
     * 绘制干扰线
     */
    private static void drawLine(Graphics g) {

        int x = RANDOM.nextInt(WIDTH);
        int y = RANDOM.nextInt(HEIGHT);

        int xl = RANDOM.nextInt(13);
        int yl = RANDOM.nextInt(15);

        g.drawLine(x, y, x + xl, y + yl);

    }

    /**
     * 获取随机的字符
     */
    public static String getRandomString(int num) {
        return String.valueOf(RANDOM_BASIC.charAt(num));
    }
}
